// jjwm/shoperDetail2/shoperDetail2.js
Page({


  /**
   * 页面的初始数据
   */
  data: {
    editTrue: false,
    curIdx:0,
    sortChange:"综合排序",
    zh: ["综合排序","起送价最低", "配送费最低", "人均高到低","人均低到高"],
    shopSort:[
      /* {
        img:"http://ttsweet.gitee.io/tiantian/img/zaocan.png",
        title:"小谷姐姐",
        salesNum:30,
        distance:8,
        time:20,
        minPrice:20,
        distribute:3.00,
        per:35.00
      },
      {
        img: "http://ttsweet.gitee.io/tiantian/img/zaocan.png",
        title: "麻辣烫",
        salesNum: 100,
        distance: 5,
        time: 20,
        minPrice: 10,
        distribute: 1.00,
        per: 15.00
      },
      {
        img: "http://ttsweet.gitee.io/tiantian/img/zaocan.png",
        title: "麦多",
        salesNum: 50,
        distance: 20,
        time: 20,
        minPrice: 20,
        distribute: 3.00,
        per: 20.00
      },
      {
        img: "http://ttsweet.gitee.io/tiantian/img/zaocan.png",
        title: "湘菜",
        salesNum: 30,
        distance: 8,
        time: 30,
        minPrice: 32,
        distribute: 0.00,
        per: 45.00
      }, */
    ]
  },

  /* tab切换点事件 */
  change(e){
    var that=this
    if (e.currentTarget.dataset.idx == 0){
      this.setData({
        curIdx: e.currentTarget.dataset.idx,
        editTrue: true
      })
    }else{
      this.setData({
        curIdx: e.currentTarget.dataset.idx,
        editTrue: false
      })
    } 
    var sortType=e.currentTarget.dataset.type
    this.sortShop(sortType)
   /*  console.log(e.currentTarget.dataset)
    console.log(this.data.shopSort) */
  },


/* 遮罩层各条件点击事件 */
  qisongjia2(e){
    var Index=e.currentTarget.dataset.index
    this.setData({
      editTrue:false,
      sortChange: this.data.zh[Index]
    })
    var sortType = e.currentTarget.dataset.type
    var sortType2 = e.currentTarget.dataset.type2
    this.sortShop(sortType, sortType2)
  },


/* 店铺按条件排序 */
  sortShop(type,type2){
    this.data.shopSort.sort(function(a,b){
      if (type == "sellNum" || type2=="per"){
        return b[type] - a[type]
      }else{
        return a[type] - b[type]
      }
    })
    this.setData({
      shopSort: this.data.shopSort
    })
  },

  //获取数据
  getData(){
    wx.showLoading({
      title: "数据加载中..."
    })
    var db = wx.cloud.database()
    var thi = this
    db.collection('shop').get({
      success:function(res){
        /* console.log(res) */
        var arr = thi.data.shopSort
        /* console.log(arr) */
        arr = arr.concat(res.data)
        thi.setData({
          shopSort: arr
        })
        wx.hideLoading() 
      }
    })
  },
  
  /* 去下单 */
  goshopping(){
    wx.navigateTo({
      url: '../shoppingDetail/shoppingDetail'
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function(e) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showLoading({
      title: "下拉刷新",
      icon: "none"
    })
    setTimeout(function () {
      wx.hideLoading()
      wx.stopPullDownRefresh()
    }, 600)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})